<!-- 
  @author: 善良的YWJ
  @Created by shanliangdeYWJ on 2021/07/07 01:50:48
  @file: ModuleButton.vue
  @description:
  ```
  teleport 是 vue3 的一个全局的组件 有一个to属性可以将模态窗附加到body而不是
  当前组件所在dom层结构
  ```
  @link [Github](https://github.com/shanliangdeYWJ)
-->

<template>
  <div class="ModalButton">
    <button @click="modalOpen = true">弹出一个模态窗口</button>
    <teleport to="body">
      <div v-if="modalOpen" class="modal">
        <div>
          这是一个弹窗 我的父元素是 body
          <button @click="modalOpen = false">关闭</button>
        </div>
      </div>
    </teleport>
  </div>
</template>

<script>
export default {
  name: "ModalButton",
  data() {
    return {
      modalOpen: false,
    };
  },
};
</script>

<style>
.modal {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.modal div {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: white;
  width: 300px;
  height: 300px;
  padding: 5px;
}
</style>
